<?php
/************************
 *  Codenova.pl Project
 *  Created by: Kamil Mrzygłód
 *
 *  (c) 2013 by Codenova.pl
 *  All Right Reserved
 ***********************/

namespace Gotcha\App\Extensions;

// Gotcha error class

use Gotcha\App\Framework\Controller;
use Gotcha\App\GotchaBootstrap;
use Gotcha\App\Types\Arrays;
use Gotcha\App\Types\String;

final class ErrorsExtension extends GotchaBootstrap {
    public static function throwError(String $msg, Arrays $params) {
        if (!parent::$errorHandlerIsSet) {
            set_error_handler(array('ErrorsExtension', 'gotchaErrorHandler'));
            parent::$errorHandlerIsSet = true;
        }

        // If we have optional parameters, we expect find
        // format signs, which will be overwritten
        $data = vsprintf($msg, $params->array);
        trigger_error($data, E_USER_ERROR);
    }

    public static function throwWarning(String $msg, Arrays $params) {
        if (!parent::$errorHandlerIsSet) {
            set_error_handler(array('ErrorsExtension', 'gotchaErrorHandler'));
            parent::$errorHandlerIsSet = true;
        }

        // If we have optional parameters, we expect find
        // format signs, which will be overwritten
        $data = vsprintf($msg, $params->array);
        trigger_error($data, E_USER_WARNING);
    }

    public static function throwNotice(String $msg, Arrays $params) {
        if (!parent::$errorHandlerIsSet) {
            set_error_handler(array('ErrorsExtension', 'gotchaErrorHandler'));
            parent::$errorHandlerIsSet = true;
        }

        // If we have optional parameters, we expect find
        // format signs, which will be overwritten
        $data = vsprintf($msg, $params->array);
        trigger_error($data, E_USER_NOTICE);
    }

    public function gotchaErrorHandler($errorNumber, $errorString, $errorFile, $errorLine) {
        if (!(error_reporting() && $errorNumber)) {
            // This error code is not included in error_reporting
            return false;
        }

        // Initialize framework
        $framework = new Controller();

        switch ($errorNumber) {
            case E_USER_ERROR:
                $framework->View(array('errorMsg' => $errorString, 'errorFile' => $errorFile, 'errorLine' => $errorLine), 'Error');
                break;

            case E_USER_WARNING:
                $framework->View(array('errorMsg' => $errorString, 'errorFile' => $errorFile, 'errorLine' => $errorLine), 'Warning');
                break;

            case E_USER_NOTICE:
                $framework->View(array('noticeMsg' => $errorString, 'noticeFile' => $errorFile, 'noticeLine' => $errorLine), 'Notice');
                break;

            case E_RECOVERABLE_ERROR:
                $framework->View(array('errorMsg' => $errorString, 'errorFile' => $errorFile, 'errorLine' => $errorLine), 'Error');
                LogsExtension::createNewLogEntry(new String('ERROR_DURING_VARIABLE_TYPING'), new Arrays(array()));
                break;

            default:
                $framework->View(array('errorMsg' => $errorString, 'errorFile' => $errorFile, 'errorLine' => $errorLine), 'Error');
                LogsExtension::createNewLogEntry(new String('UNKNOWN_ERROR'), new Arrays(array()));
                break;
        }

        // Don't execute PHP internal error handler
        return true;
    }

    public static function throwErrorAndSaveLog(String $translationId, Arrays $params) {
        LogsExtension::createNewLogEntry(new String(CulturesExtension::getTranslation($translationId)), $params);
        self::throwError(new String(CulturesExtension::getTranslation($translationId)), $params);
        exit;
    }

    public static function throwErrorAndSaveLogWithoutCulture(String $errorMsg, Arrays $params) {
        LogsExtension::createNewLogEntry($errorMsg, $params);
        self::throwError($errorMsg, $params);
        exit;
    }

    public static function throwErrorWithoutLog(String $translationId, Arrays $param) {
        self::throwError(new String(CulturesExtension::getTranslation($translationId)), $param);
        exit;
    }

    public static function throwWarningAndSaveLog(String $translationId, Arrays $params) {
        LogsExtension::createNewLogEntry(new String(CulturesExtension::getTranslation($translationId)), $params);
        self::throwWarning(new String(CulturesExtension::getTranslation($translationId)), $params);
    }

    public static function throwNoticeWithoutLog(String $translationId, Arrays $params) {
        self::throwNotice(new String(CulturesExtension::getTranslation($translationId)), $params);
        exit;
    }
}